package be.destin.abdera;

import org.apache.abdera.protocol.server.CollectionInfo;
import org.apache.abdera.protocol.server.RequestContext;
import org.apache.abdera.protocol.server.RequestContext.Scope;
import org.apache.abdera.protocol.server.WorkspaceInfo;
import org.apache.abdera.protocol.server.impl.DefaultWorkspaceManager;
import org.apache.abdera.protocol.server.impl.SimpleWorkspaceInfo;
import org.apache.abdera.protocol.server.provider.basic.BasicProvider;

import be.destin.skos.core.Concept;
import be.destin.skos.core.NoScheme;
import be.destin.skos.core.SchemeInterface;
import be.destin.skos.core.SkosManager;

public class ConceptProvider extends AbstractCorrectedWorkspaceProvider {

    SkosManager skosManager;

	/**
	 * 
	 */
	public ConceptProvider(String host, String base, WorkspaceInfo wi, SkosManager skosManager) {
		super();
		this.skosManager = skosManager;
		this.init (host, base, wi);
	}

    @Override
    public ConceptCollectionAdapter getCollectionAdapter(RequestContext request) {
        //String path = request.getContextPath() + request.getTargetPath();
        // Typically this happens when a Resolver wants to override the CollectionAdapter being used
        ConceptCollectionAdapter ca = (ConceptCollectionAdapter)request.getAttribute(Scope.REQUEST, DefaultWorkspaceManager.COLLECTION_ADAPTER_ATTRIBUTE);
        if (ca != null) {
            return ca;
        }
        String coll = request.getTarget().getParameter(BasicProvider.PARAM_FEED);
        if (coll == null || coll.isEmpty()) return null;

        for (CollectionInfo ci : this.workspace.getCollections(request)) {
        	String ciColl = ((ConceptCollectionAdapter)ci).getHref();
//log.error(ci.getHref(request)+",col="+ciColl+" = "+coll);
        	if (coll.equalsIgnoreCase(ciColl)) {
        		/* Caches access to the CollectionAdapter */
        		request.setAttribute( DefaultWorkspaceManager.COLLECTION_ADAPTER_ATTRIBUTE, ci);
        		return (ConceptCollectionAdapter)ci;
        	}
        }
        if (coll != null && !coll.isEmpty()) {
        	SchemeInterface aScheme = skosManager.openScheme(coll);
        	if (aScheme instanceof NoScheme && aScheme.getClass() != NoScheme.class) {
        		ca = new ConceptCollectionAdapter(skosManager, (NoScheme)aScheme); 
        		for (WorkspaceInfo wi: this.getWorkspaces(request)) {
        			((SimpleWorkspaceInfo)wi).addCollection(ca);
        			
        			break;
        		}
        	}
        }
        return ca;
    }

    public ConceptCollectionAdapter getCollectionAdapter(RequestContext request,Concept aConcept) {
    	String scheme = aConcept.getInScheme().getAbout().toLowerCase();
    	for (CollectionInfo ci : this.workspace.getCollections(request)) {
    		if (scheme.equals(((ConceptCollectionAdapter)ci).getScheme().getAbout().toLowerCase())) {
    			return (ConceptCollectionAdapter)ci;
    		}
    	}
    	return null;
    }

    public SkosManager getSkosManager() {
		return skosManager;
	}


	public void setSkosManager(SkosManager skosManager) {
		this.skosManager = skosManager;
	}


    public ConceptSearchUrlAdapter newSearchUrlAdapter() {
    	return new ConceptSearchUrlAdapter(this, skosManager);
    }
}
